<template>
  <default-layout>
    <header-close text="修改密码" slot="header"></header-close>
    <div class="form password" slot="main">
      <yd-cell-group>
        <div class="top-label">旧密码</div>
        <yd-cell-item>
          <xf-input
            :valid="[{required: true, msg: '请输入旧密码'}]"
            v-model="form.currentPwd"
            ref="currentPwd"
            slot="right"
            type="password"
            placeholder="请输入旧密码"></xf-input>
        </yd-cell-item>
        <div class="top-label">新密码</div>
        <yd-cell-item>
          <xf-input
            :valid="[{required: true, msg: '请输入新密码'}, {reg: $regex.password, msg: '密码只能是数字、字母、下划线，6-16位'}]"
            v-model="form.newPwd"
            ref="newPwd"
            slot="right"
            type="password"
            placeholder="请输入新密码"></xf-input>
        </yd-cell-item>
        <yd-cell-item>
          <xf-input
            :valid="[{required: true, msg: '请再次输入新密码'}]"
            v-model="form.rPwd"
            ref="rPwd"
            slot="right"
            type="password"
            placeholder="请再次输入新密码"></xf-input>
        </yd-cell-item>
      </yd-cell-group>
    </div>
    <div class="next" slot="footer">
      <mt-button type="primary" size="large" @click="onSubmit">完成</mt-button>
    </div>
  </default-layout>
</template>
<script>
  import HeaderClose from '../../../components/header-close/header-close'
  import XfInput from '../../../components/xf-input/xf-input.vue'
  import DefaultLayout from '../../../layouts/default-layout/default-layout'
  import {validate} from '../../../common/js/util'
  import {mapGetters} from 'vuex'

  export default {
    components: {
      DefaultLayout,
      XfInput,
      HeaderClose},
    computed: {
      ...mapGetters([
        'userInfo'
      ])
    },
    data() {
      return {
        form: {
          currentPwd: '',
          newPwd: '',
          rPwd: ''
        }
      }
    },
    methods: {
      onSubmit() {
        validate.bind(this)(this.form).then(() => {
          if (this.form.newPwd !== this.form.rPwd) {
            this.$message.toast('两次密码输入不一致')
            return
          }
          if (this.userInfo.role === 'corp') {
            this.corp()
          } else {
            this.user()
          }
        })
      },
      user() {
        this.$post('/admin/person/sapi/person/center/my/config/password/edit', this.form, loading => { this.$message.loading(loading) }).then(res => {
          this.$message.alert(res.message).then(() => {
            this.$router.back()
          })
        })
      },
      corp() {
        this.$post('/sapi/public/system/corp/password/put', {
          corpId: this.userInfo.aab001,
          password: this.form.currentPwd,
          newPassword: this.form.newPwd
        }).then(res => {
          this.$message.alert(res.message).then(() => {
            this.$router.back()
          })
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  @import "../../../common/style/variable";
  .password{
    background: #ededed;
  }
  .top-label{
    padding: .2rem .2rem;
    background: #ededed;
  }
</style>
